package com.example.tool.util.yto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ResponseParser {
    public static void parseResponse(String response) {
        try {
            // 尝试解析为数组格式（成功响应）
            JSONArray trackArray = JSONArray.parseArray(response);

            System.out.println("物流轨迹信息:");
            for (int i = 0; i < trackArray.size(); i++) {
                JSONObject item = trackArray.getJSONObject(i);
                System.out.println("--------------------------------");
                System.out.println("运单号: " + item.getString("waybill_No"));
                System.out.println("时间: " + item.getString("upload_Time"));
                System.out.println("状态: " + getStatusDesc(item.getString("infoContent")));
                System.out.println("详情: " + item.getString("processInfo"));

                if (item.containsKey("city")) {
                    System.out.println("城市: " + item.getString("city"));
                }
                if (item.containsKey("district")) {
                    System.out.println("区县: " + item.getString("district"));
                }
                if (item.containsKey("weight")) {
                    System.out.println("重量: " + item.getDouble("weight") + "kg");
                }
            }
        } catch (Exception e) {
            // 如果不是数组格式，尝试解析为对象格式（空响应）
            JSONObject responseObj = JSONObject.parseObject(response);
            if ("1001".equals(responseObj.getString("code"))) {
                System.out.println("查询结果为空: " + responseObj.getString("message"));
            } else {
                System.out.println("查询失败: " + response);
            }
        }
    }

    private static String getStatusDesc(String statusCode) {
        switch (statusCode) {
            case "GOT": return "已揽收";
            case "ARRIVAL": return "已收入";
            case "DEPARTURE": return "已发出";
            case "SENT_SCAN": return "派件";
            case "INBOUND": return "自提柜入柜";
            case "SIGNED": return "签收成功";
            case "FAILED": return "签收失败";
            case "FORWARDING": return "转寄";
            case "TMS_RETURN": return "退回";
            case "AIRSEND": return "航空发货";
            case "AIRPICK": return "航空提货";
            default: return statusCode;
        }
    }
}